﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;

namespace EmailSender
{
    public class EmailWaper
    {
        //public EventHandler handler;
        public event EventHandler<EventArgs> OnSendSucess;


        MailMessage message;
        SmtpClient smtpClient;
        MailAddress addressFrom;
        string sendTo;
        public string SendTo { get { return sendTo; } }
        string subject;
        string content;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fromAddress">显示的发件人email地址</param>
        /// <param name="displayName">邮件中显示的发件人姓名</param>
        /// <param name="host">smtp服务地址</param>
        /// <param name="userName">smtp用户名</param>
        /// <param name="userPassword">smtp用户密码</param>
        /// <param name="sendTo">收件人地址</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="content">邮件正文</param>
        public EmailWaper(string fromAddress, string displayName, string host, string userName,string userPassword, 
            string sendTo,string subject,string content)
        {
            addressFrom = new MailAddress(fromAddress, displayName);
            
            smtpClient = new SmtpClient();
            smtpClient.Credentials = new System.Net.NetworkCredential(userName, userPassword);
            smtpClient.Host = host;
            this.sendTo = sendTo;
            this.subject = subject;
            this.content = content;            
        }

        public void Send()
        {
            try
            {
                message = new MailMessage();

                message.From = addressFrom;
                message.To.Add(sendTo);
                message.Subject = subject;
                message.Body = content;
                message.IsBodyHtml = true;

                smtpClient.Send(message);

                if (OnSendSucess != null)
                {
                    OnSendSucess(this, null);
                }
            }
            catch
            {
 
            }
        }
    }
}
